package org.dandsoft.blackbox.laboratory.tools;

import org.dandsoft.blackbox.snn.*;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import java.util.Map;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 03.04.2009 10:11:21
 */
public class StoreUtil {

    public static Map<ObjectId, Signal> restoreSignals(Node signalsNode) throws RepositoryException {
        if (signalsNode != null) {
            NodeIterator iterator = signalsNode.getNodes();
            Map<ObjectId, Signal> result = new Hashtable<ObjectId, Signal>();
            while (iterator.hasNext()) {
                Node node = iterator.nextNode();
                Signal signal = StoreUtil.restoreSignal(node);
                result.put(signal.getSignalId(), signal);
            }
            return result;
        } else {
            return new Hashtable<ObjectId, Signal>();
        }
    }

    public static void storeSignal(Node node, Signal signal) throws RepositoryException {
        Node signalNode = node.addNode(Integer.toString(signal.getSignalId().getValue()));
        signalNode.setProperty("value", signal.getValue().getValue());
        signalNode.setProperty("id", signal.getSignalId().getValue());
    }

    public static Signal restoreSignal(Node signalNode) throws RepositoryException {
        if (signalNode != null) {
            ObjectId id = new ObjectId((int) signalNode.getProperty("id").getLong());
            Value value = new Value((float) signalNode.getProperty("value").getDouble());
            return new Signal(id, value);
        } else {
            return null;
        }
    }

    public static void storeTrigger(Node node, Trigger trigger) throws RepositoryException {
        Node newTriggerNode = node.addNode(Integer.toString(trigger.getId().getValue()));
        newTriggerNode.setProperty("id", trigger.getId().getValue());
        newTriggerNode.setProperty("state", trigger.getState().name());
        for (Synapse synapse : trigger.getSynapses()) {
            Node synapseNode = newTriggerNode.addNode(Integer.toString(synapse.getSignalId().getValue()));
            synapseNode.setProperty("signalId", synapse.getSignalId().getValue());
            synapseNode.setProperty("isUseful", synapse.isUseful());
            if (synapse.getMinValue() != null) {
                synapseNode.setProperty("minValue", synapse.getMinValue().getValue());
            }
            if (synapse.getMaxValue() != null) {
                synapseNode.setProperty("maxValue", synapse.getMaxValue().getValue());
            }
            if (synapse.getWeight() != null) {
                synapseNode.setProperty("weight", synapse.getWeight().getValue());
            }
            if (synapse.getTime() != null) {
                synapseNode.setProperty("time", synapse.getTime().getValue());
            }
            if (synapse.getValue() != null) {
                synapseNode.setProperty("value", synapse.getValue().getValue());
            }
        }
    }

    public static Trigger restoreTrigger(Node node) throws RepositoryException {
        if (node != null) {
            Trigger trigger = new Trigger();

            trigger.setId(new ObjectId(Integer.parseInt(node.getProperty("id").getString())));
            trigger.setState(Trigger.State.valueOf(node.getProperty("state").getString()));

            NodeIterator synapses = node.getNodes();
            while (synapses.hasNext()) {
                Node synapseNode = synapses.nextNode();
                Synapse synapse = new Synapse();
                synapse.setSignalId(new ObjectId((int) synapseNode.getProperty("signalId").getLong()));
                synapse.setUseful(synapseNode.getProperty("isUseful").getBoolean());
                try {
                    synapse.setMinValue(new Value((float) synapseNode.getProperty("minValue").getDouble()));
                } catch (PathNotFoundException e) {
                }

                try {
                    synapse.setMaxValue(new Value((float) synapseNode.getProperty("maxValue").getDouble()));
                } catch (PathNotFoundException e) {
                }

                try {
                    synapse.setWeight(new Value((float) synapseNode.getProperty("weight").getDouble()));
                } catch (PathNotFoundException e) {
                }

                try {
                    synapse.setTime(new Time(synapseNode.getProperty("time").getLong()));
                } catch (PathNotFoundException e) {
                }

                try {
                    synapse.setValue(new Value((float) synapseNode.getProperty("value").getDouble()));
                } catch (PathNotFoundException e) {
                }
                trigger.getSynapses().add(synapse);
            }

            return trigger;
        } else {
            return null;
        }
    }
}
